package cn.com.egova.demoapplication;

import android.app.ActionBar;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.egova.demoapplication.base.BaseActivity;

public class TweenAnimaActivity extends BaseActivity {
    public static final String TAG = "TweenAnimaActivity";

    @InjectView(R.id.btn_alpha_xml)
    Button btnAlphaXml;
    @InjectView(R.id.btn_translate_xml)
    Button btnTranslateXml;
    @InjectView(R.id.btn_rotate_xml)
    Button btnRotateXml;
    @InjectView(R.id.btn_scale_xml)
    Button btnScaleXml;
    @InjectView(R.id.btn_combile_xml)
    Button btnCombileXml;
    @InjectView(R.id.btn_alpha_java)
    Button btnAlphaJava;
    @InjectView(R.id.btn_translate_java)
    Button btnTranslateJava;
    @InjectView(R.id.btn_rotate_java)
    Button btnRotateJava;
    @InjectView(R.id.btn_scale_java)
    Button btnScaleJava;
    @InjectView(R.id.btn_combile_java)
    Button btnCombileJava;
    @InjectView(R.id.iv)
    ImageView iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_anima);
        ButterKnife.inject(this);

    }

    @Override
    public boolean isBackIconVisible() {
        return true;
    }

    @OnClick({R.id.btn_alpha_xml, R.id.btn_translate_xml, R.id.btn_rotate_xml, R.id.btn_scale_xml, R.id.btn_combile_xml})
    public void animationByXml(View view) {
        int resid = 0;
        switch (view.getId()) {
            case R.id.btn_alpha_xml:
                resid = R.anim.animation_alpha;
                break;
            case R.id.btn_translate_xml:
                resid = R.anim.animation_translate;
                break;
            case R.id.btn_rotate_xml:
                resid = R.anim.animation_rotate;
                break;
            case R.id.btn_scale_xml:
                resid = R.anim.animation_scale;
                break;
            case R.id.btn_combile_xml:
                resid = R.anim.animation_combile;
                break;
        }

        Animation animation = AnimationUtils.loadAnimation(this, resid);
        iv.startAnimation(animation);

        //警惕:一下方式不存在!!!
//        iv.setAnimation(voice);
//        voice.start();
    }


    @OnClick({R.id.btn_alpha_java, R.id.btn_translate_java, R.id.btn_rotate_java, R.id.btn_scale_java, R.id.btn_combile_java})
    public void animationByJava(View view) {
        switch (view.getId()) {
            case R.id.btn_alpha_java:
                Animation animation1 = new AlphaAnimation(1.0f, 0.1f);
                animation1.setDuration(5000);
                animation1.setFillAfter(false);
                iv.startAnimation(animation1);
                break;

            case R.id.btn_translate_java:
                //下面两种形式均可以, 四参数的默认所有的Type都是RELATIVE_TO_SELF.
//                voice = new TranslateAnimation(0,300,0,300);
                Animation animation2 = new TranslateAnimation(
                        Animation.RELATIVE_TO_SELF,
                        -1.0f,
                        Animation.RELATIVE_TO_SELF,
                        1.0f,
                        Animation.RELATIVE_TO_SELF,
                        -1.0f,
                        Animation.RELATIVE_TO_SELF,
                        1.0f);
                animation2.setDuration(5000);
                animation2.setFillAfter(false);
                iv.startAnimation(animation2);
                break;

            case R.id.btn_rotate_java:
                Animation animation3 = new RotateAnimation(
                        0f,
                        360f,
                        Animation.RELATIVE_TO_SELF,
                        0.5f,
                        Animation.RELATIVE_TO_SELF,
                        0.5f
                );
                animation3.setDuration(5000);
                animation3.setFillAfter(false);
                iv.startAnimation(animation3);
                break;

            case R.id.btn_scale_java:
                Animation animation4 = new ScaleAnimation(
                        0f,
                        1.5f,
                        0f,
                        1.5f,
                        Animation.RELATIVE_TO_SELF,
                        0.5f,
                        Animation.RELATIVE_TO_SELF,
                        0.5f
                );
                animation4.setDuration(5000);
                animation4.setFillAfter(false);
                iv.startAnimation(animation4);
                break;

            case R.id.btn_combile_java:
                Animation anima1 = new AlphaAnimation(1.0f, 0.1f);
                Animation anima2 = new TranslateAnimation(0, 300, 0, 300);
                anima2.setInterpolator(new BounceInterpolator()); //加一个结束时弹起的插值器.
                Animation anima3 = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                        0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                Animation anima4 = new ScaleAnimation(0f, 1.5f, 0f, 1.5f, Animation.RELATIVE_TO_SELF,
                        0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

                AnimationSet set = new AnimationSet(true);
                set.addAnimation(anima1);
                set.addAnimation(anima2);
                set.addAnimation(anima3);
                set.addAnimation(anima4);
                set.setDuration(5000);
                set.setFillAfter(false);
                iv.startAnimation(set);
                break;
        }
    }


    /**
     * 设置是否在菜单栏显示返回按钮图标.
     *
     * @param isVisible
     */
    public void setBackIconVisible(boolean isVisible) {
        ActionBar actionBar = getActionBar();
        if (null != actionBar) {
            actionBar.setDisplayHomeAsUpEnabled(isVisible);
            actionBar.setDisplayShowHomeEnabled(false); //默认不显示应用程序图标.
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
